/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ComboitemRenderer;

/**
 * @author JS: Juan Sanjuan
 * This Class is used as an utiliti to create comboboxes and their 
 * default params
 */
public class RenderizadorCombo implements ComboitemRenderer
{
    private String strId;
    private String strValor;
    private String strValorxDefecto;
    private Combobox objCombo;
    
    public RenderizadorCombo(String _strId, String _strValor, String _strValorxDefecto, Combobox _objCombo)
    {
        strId=_strId;
        strValor=_strValor;
        strValorxDefecto=_strValorxDefecto;
        objCombo =_objCombo;
    }
    
    @Override
    public void render(Comboitem item, Object data) throws Exception {
        //data.getClass().
        String strRetornoId; 
        String strRetornoValor;
        try
        {
            Method objMetodoId=data.getClass().getMethod(strId);
            strRetornoId=objMetodoId.invoke(data).toString();
            Method objMetodoValor=data.getClass().getMethod(strValor);
            strRetornoValor=objMetodoValor.invoke(data).toString();
            item.setLabel(strRetornoValor);
            item.setValue(strRetornoId);
            if(strValorxDefecto!=null)
            {
                if(strRetornoId.equals(strValorxDefecto))
                {
                    objCombo.setSelectedItem(item);
                }   
            }
        }catch (Exception ex)
        {
            Logger.getLogger(IndiceControlador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
